

#include "stdafx.h"
#include "pf_dimlinear.h"
#include "pf_constructionline.h"
#include "pf_text.h"
#include "pf_solid.h"
#include "pf_graphic.h"


/**
 * Constructor.
 *
 * @para parent Parent Entity Container.
 * @para d Common dimension geometrical data.
 * @para ed Extended geometrical data for linear dimension.
 */
PF_DimLinear::PF_DimLinear(PF_Container* parent,
                           const PF_DimensionAttribute& d,
                           const PF_DimLinearAttribute& ed)
        : PF_Dimension(parent, d), edata(ed) {

    calculateBorders();
}



PF_VectorSolutions PF_DimLinear::getRefPoints() {
	PF_VectorSolutions ret(edata.extensionPoint1, edata.extensionPoint2,
						data.definitionPoint, data.middleOfText);
	return ret;
}


/**
 * @return Automatically created label for the default 
 * measurement of this dimension.
 */
QString PF_DimLinear::getMeasuredLabel() {
    // direction of dimension line
    PF_Vector dirDim;
    dirDim.setPolar(100.0, edata.angle);

    // construction line for dimension line
    PF_ConstructionLine dimLine(NULL,
                                PF_ConstructionLineAttribute(data.definitionPoint,
                                                        data.definitionPoint + dirDim));

    PF_Vector dimP1 = dimLine.getNearestPointOnEntity(edata.extensionPoint1);
    PF_Vector dimP2 = dimLine.getNearestPointOnEntity(edata.extensionPoint2);

    // Definitive dimension line:
    double dist = dimP1.distanceTo(dimP2);

	PF_Graphic* graphic = getGraphic();

    QString ret;
	if (graphic!=NULL) {
		ret = PF_Units::formatLinear(dist, graphic->getUnit(), 
			graphic->getLinearFormat(), graphic->getLinearPrecision());
	}
	else {
        ret = QString("%1").arg(dist);
	}

    return ret;
}



bool PF_DimLinear::hasEndpointsWithinWindow(PF_Vector v1, PF_Vector v2) {
	return (edata.extensionPoint1.isInWindow(v1, v2) ||
	        edata.extensionPoint2.isInWindow(v1, v2));
}



/**
 * Updates the sub entities of this dimension. Called when the 
 * text or the position, alignment, .. changes.
 *
 * @param autoText Automatically reposition the text label
 */
void PF_DimLinear::update(bool autoText) {

    PF_DEBUG->print("PF_DimLinear::update");

    clear();

	if (isUndone()) {
		return;
	}

    // distance from entities (DIMEXO)
    double dimexo = getExtensionLineOffset();
    // extension line extension (DIMEXE)
    double dimexe = getExtensionLineExtension();

    PF_LineAttribute ld;
    double extAngle = edata.angle + (M_PI/2.0);

    // direction of dimension line
    PF_Vector dirDim;
    dirDim.setPolar(100.0, edata.angle);
    // direction of extension lines
    PF_Vector dirExt;
    dirExt.setPolar(100.0, extAngle);

    // construction line for dimension line
    PF_ConstructionLine dimLine(
        NULL,
        PF_ConstructionLineAttribute(data.definitionPoint,
                                data.definitionPoint + dirDim));

    PF_Vector dimP1 = dimLine.getNearestPointOnEntity(edata.extensionPoint1);
    PF_Vector dimP2 = dimLine.getNearestPointOnEntity(edata.extensionPoint2);

    // Definitive dimension line:
    updateCreateDimensionLine(dimP1, dimP2, true, true, autoText);
    /*
    ld = PF_LineData(data.definitionPoint, dimP1);
    PF_Line* dimensionLine = new PF_Line(this, ld);
       addEntity(dimensionLine);
    */
    PF_Vector vDimexo1, vDimexe1, vDimexo2, vDimexe2;
    vDimexe1.setPolar(dimexe, edata.extensionPoint1.angleTo(dimP1));
    vDimexo1.setPolar(dimexo, edata.extensionPoint1.angleTo(dimP1));

    vDimexe2.setPolar(dimexe, edata.extensionPoint2.angleTo(dimP2));
    vDimexo2.setPolar(dimexo, edata.extensionPoint2.angleTo(dimP2));
	
    if ((edata.extensionPoint1-dimP1).magnitude()<1e-6) {
        vDimexe1.setPolar(dimexe,
                          data.definitionPoint.angleTo(dimP1)-M_PI/2.0);
        vDimexo1.setPolar(dimexo,
                          data.definitionPoint.angleTo(dimP1)-M_PI/2.0);
    }
    if ((edata.extensionPoint2-dimP2).magnitude()<1e-6) {
        vDimexe2.setPolar(dimexe,
                          data.definitionPoint.angleTo(dimP2)-M_PI/2.0);
        vDimexo2.setPolar(dimexo,
                          data.definitionPoint.angleTo(dimP2)-M_PI/2.0);
    }
	
    // extension lines:
    ld = PF_LineAttribute(edata.extensionPoint1+vDimexo1,
                     dimP1+vDimexe1);
    PF_Line* line = new PF_Line(this, ld);
    line->setPen(PF_Pen(LX_Define::FlagInvalid));
    line->setLayer(NULL);
    addEntity(line);
    ld = PF_LineAttribute(edata.extensionPoint2+vDimexo2,
                     dimP2+vDimexe2);
    //data.definitionPoint+vDimexe2);
    line = new PF_Line(this, ld);
    line->setPen(PF_Pen(LX_Define::FlagInvalid));
    line->setLayer(NULL);
    addEntity(line);

    calculateBorders();
}



void PF_DimLinear::move(PF_Vector offset) {
    PF_Dimension::move(offset);

    edata.extensionPoint1.move(offset);
    edata.extensionPoint2.move(offset);
    update();
}



void PF_DimLinear::rotate(PF_Vector center, double angle) {
    PF_Dimension::rotate(center, angle);

    edata.extensionPoint1.rotate(center, angle);
    edata.extensionPoint2.rotate(center, angle);
    edata.angle = PF_Math::correctAngle(edata.angle+angle);
    update();
}



void PF_DimLinear::scale(PF_Vector center, PF_Vector factor) {
    PF_Dimension::scale(center, factor);

    edata.extensionPoint1.scale(center, factor);
    edata.extensionPoint2.scale(center, factor);
    update();
}



void PF_DimLinear::mirror(PF_Vector axisPoint1, PF_Vector axisPoint2) {
    PF_Dimension::mirror(axisPoint1, axisPoint2);

    edata.extensionPoint1.mirror(axisPoint1, axisPoint2);
    edata.extensionPoint2.mirror(axisPoint1, axisPoint2);

    PF_Vector vec;
    vec.setPolar(1.0, edata.angle);
    vec.mirror(PF_Vector(0.0,0.0), axisPoint2-axisPoint1);
    edata.angle = vec.angle();

    update();
}



void PF_DimLinear::stretch(PF_Vector firstCorner,
                           PF_Vector secondCorner,
                           PF_Vector offset) {

    //e->calculateBorders();
    if (getMin().isInWindow(firstCorner, secondCorner) &&
            getMax().isInWindow(firstCorner, secondCorner)) {

        move(offset);
    } else {
        //PF_Vector v = data.definitionPoint - edata.extensionPoint2;
        //double len = edata.extensionPoint2.distanceTo(data.definitionPoint);
        //double ang1 = edata.extensionPoint1.angleTo(edata.extensionPoint2)
        //              + M_PI/2;

        if (edata.extensionPoint1.isInWindow(firstCorner,
                                            secondCorner)) {
            edata.extensionPoint1.move(offset);
        }
        if (edata.extensionPoint2.isInWindow(firstCorner,
                                            secondCorner)) {
            edata.extensionPoint2.move(offset);
        }

		/*
        double ang2 = edata.extensionPoint1.angleTo(edata.extensionPoint2)
                      + M_PI/2;

        double diff = PF_Math::getAngleDifference(ang1, ang2);
        if (diff>M_PI) {
            diff-=2*M_PI;
        }

        if (fabs(diff)>M_PI/2) {
            ang2 = PF_Math::correctAngle(ang2+M_PI);
        }

        PF_Vector v;
        v.setPolar(len, ang2);
        data.definitionPoint = edata.extensionPoint2 + v;
		*/
    }
    update(true);
}



void PF_DimLinear::moveRef(const PF_Vector& ref, const PF_Vector& offset) {

    if (ref.distanceTo(data.definitionPoint)<1.0e-4) {
        data.definitionPoint += offset;
		update(true);
    }
	else if (ref.distanceTo(data.middleOfText)<1.0e-4) {
        data.middleOfText += offset;
		update(false);
    }
	else if (ref.distanceTo(edata.extensionPoint1)<1.0e-4) {
        edata.extensionPoint1 += offset;
		update(true);
    }
	else if (ref.distanceTo(edata.extensionPoint2)<1.0e-4) {
        edata.extensionPoint2 += offset;
		update(true);
    }
}

/**
 * Dumps the point's data to stdout.
 */
std::ostream& operator << (std::ostream& os, const PF_DimLinear& d) {
    os << " DimLinear: " << d.getData() << "\n" << d.getEData() << "\n";
    return os;
}

